import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'


import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import {isLogin,errorTip} from './utils/tools'

const app = createApp(App);
app.use(ElementPlus);
app.use(store).use(router).mount('#app')
router.beforeEach((to,form,next) =>{
    if(to.path == '/login' || to.path == '/' ){     
        next();
    }else{
        const token = localStorage.getItem('TOKEN');
        const res = isLogin(token as string)
        res.then(v=>{
            if(v.status==1){
                store.dispatch('setToken','')
                store.dispatch('setUserId','')
                store.dispatch('setLoginState',0)
                errorTip('请先登录')
                router.push('/login')
            }else{
                store.dispatch('setToken',token)
                store.dispatch('setUserId',v.userId)
                next()
            }
        },e=>{
            console.log(e)
        })
    }
})
